package model.pieces;

import model.memento.BoxUndoableEdit;
import model.memento.Movement;
import model.memento.MovementDirection;

public class Box extends Piece {

	/**
	 * @param x <-> col
	 * @param y <-> row
	 */
	public Box(int x, int y) {
		super(x, y);
	}

	/**
	 * @see model.pieces.Piece#canMove()
	 */
	@Override
	public boolean canMove() {
		return true;
	}
	
	/**
	 * @see model.pieces.Piece#requestMultiMovement(model.memento.Movement)
	 */
	@Override
	public void requestMultiMovement(Movement move) {
		if (move.isMovementPosition(col, row)) {
			// notificar memento
			addMoveToMemento(move.getDirection());
			// Dejo la posicion actual, lo notifico a los Target para que se desocupen.
			this.mediator.leavePosition(this, this.col, this.row);
			// Movimiento a realizar.
			Movement newMove = move.getNextMovement();
			setPosition(newMove);
			// Notifico a todas las piezas para que protesten si ocupo su lugar y no lo permite.
			this.mediator.requestSingleMovement(this, newMove);
		}
	}
	
	private void addMoveToMemento(MovementDirection direction) {
		this.mementoSupport.postEdit(new BoxUndoableEdit(this, direction));
	}
	
	/**
	 * @see model.pieces.Piece#refuseMovement(model.memento.Movement)
	 */
	@Override
	public void refuseMovement(Movement move) {
		if (move.isMovementPosition(col, row)) {
			// Deshacer el movimiento realizado porque una pieza ha protestado
			Movement backMove = move.getBackMovement();
			setPosition(backMove);
			this.mediator.refuseMovement(this, backMove);
		}
	}
}
